package cn.itsource.product.service.impl;

import cn.itsource.basic.service.IBaiduAiAuditService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.product.domain.Product;
import cn.itsource.product.domain.ProductDetail;
import cn.itsource.product.domain.ProductOnlineAuditLog;
import cn.itsource.product.mapper.ProductDetailMapper;
import cn.itsource.product.mapper.ProductMapper;
import cn.itsource.product.mapper.ProductOnlineAuditLogMapper;
import cn.itsource.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    @Transactional
    public void add(Product product) {
        super.add(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            //将产品表的id设置到产品详情中的productid
            productDetail.setProductId(product.getId());
            //添加产品详情
            productDetailMapper.add(productDetail);
        }
    }

    @Override
    @Transactional
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            //修改产品详情
            productDetailMapper.update(productDetail);
        }

    }

    @Override
    @Transactional
    public void delete(Long id) {
        super.delete(id);
        productDetailMapper.delByDetailId(id);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            //查询出一个product对象
            Product product = productMapper.findById(id);
            //1.上架审核【内容审核，图像审核】 - 每一个宠物
            //获取宠物图片与文本并进行审核
            Boolean imageAudit = baiduAiAuditService.imageAudit(product.getResources());
            Boolean textAudit = baiduAiAuditService.textAudit(product.getName());
            //获取宠物审核日志
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            //设置宠物日志中的productId
            log.setProductId(id);
            //如果图片审核和文本审核都通过
            if (imageAudit && textAudit){
                //改状态
                productMapper.onsale(id,new Date());
                //审核成功：状态改为1 ，要添加审核日志 - 审核成功为1
                log.setState(1);
                log.setNote("审核成功");
                //将审核信息添加到审核表里
                productOnlineAuditLogMapper.add(log);
            }else {
                //2.审核失败：状态不改 ，要添加审核日志 - 审核失败为0
                log.setState(0);
                log.setNote("审核失败");
                productOnlineAuditLogMapper.add(log);
                return new AjaxResult().setSuccess(false).setMessage("上架失败");
            }
        }
        return new AjaxResult();
    }

    @Override
    public AjaxResult offsale(List<Long> ids) {
        productMapper.offsale(ids,new Date());
        return new AjaxResult();
    }
}
